clear all
set more off 

cd "C:\Dropbox\Average_target_survey\replication_folder\dofiles"
quiet do load_rreg2


cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step003, clear

*** winsorize/drop unrealistic observations
winsor2 edy_12m, cuts(2 98) suffix(_w)
winsor2 edy_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen edy_12m_w_X_IT=edy_12m_w*treatment_IT
gen edy_12m_w_X_AIT=edy_12m_w*treatment_AIT

*** winsorize/drop unrealistic observations
winsor2 egdp_12m, cuts(2 98) suffix(_w)
winsor2 egdp_5y, cuts(2 98) suffix(_w)


*** interaction of priors and posteriors
gen egdp_12m_w_X_IT=egdp_12m_w*treatment_IT
gen egdp_12m_w_X_AIT=egdp_12m_w*treatment_AIT

*** probability that inflation is greater than 4 percent (prior)
gen prob_prior=	epi_12m_12plus + epi_12m_8_12 + epi_12m_4_8

*** probability that inflation is greater than 5 percent (posterior)
gen prob_post=QJH18_1
destring prob_post, force replace


*** winsorize/drop unrealistic observations
winsor2 prob_post, cuts(2 98) suffix(_w)
winsor2 prob_prior, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen prob_prior_w_X_IT=prob_prior_w*treatment_IT
gen prob_prior_w_X_AIT=prob_prior_w*treatment_AIT


*** winsorize/drop unrealistic observations
winsor2 epi_12m, cuts(2 98) suffix(_w)
winsor2 epi_5y, cuts(2 98) suffix(_w)

*** interaction of priors and posteriors
gen epi_12m_w_X_IT=epi_12m_w*treatment_IT
gen epi_12m_w_X_AIT=epi_12m_w*treatment_AIT

gen epi_12m_mean_X_IT=epi_12m_mean*treatment_IT
gen epi_12m_mean_X_AIT=epi_12m_mean*treatment_AIT

*** compute the difference non-parametrically

tempname 1
postfile `1'  ///
	str20 outcome ///
	str10 b_treatment_IT ///
	str10 b_treatment_AIT ///
	str10 b_treatment_ITAIT ///
	str10 pval_IT_AIT ///	
	str10 pval_IT_ITAIT ///	
	str10 pval_AIT_AIT ///	
	N_reg ///
	str10 R2_reg ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\AppendixTable09_OLS.dta", replace every(1)
	
tempname 2
postfile `2'  ///
	str20 outcome ///
	str10 b_treatment_IT ///
	str10 b_treatment_AIT ///
	str10 b_treatment_ITAIT ///	
	str10 pval_IT_AIT ///	
	str10 pval_IT_ITAIT ///	
	str10 pval_AIT_AIT ///	
	N_reg ///
	str10 R2_reg ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\AppendixTable09_Huber.dta", replace every(1)
	
*===============================================================================
*			regressions forvariables with priors
*===============================================================================

foreach var in epi_12m_w prob_prior_w egdp_12m_w edy_12m_w {
	
	capture drop POSTERIOR
	if "`var'"=="epi_12m_w" gen POSTERIOR=epi_5y_w
	if "`var'"=="epi_12m_mean" gen POSTERIOR=epi_5y_w
	if "`var'"=="prob_prior_w" gen POSTERIOR=prob_post_w
	if "`var'"=="egdp_12m_w" gen POSTERIOR=egdp_5y_w
	if "`var'"=="edy_12m_w" gen POSTERIOR=edy_5y_w
	
	capture drop wgtXb2
	
	capture drop _*
	gen _POSTERIOR=POSTERIOR*sqrt(weight)
	gen _treatment_IT=treatment_IT*sqrt(weight)
	gen _treatment_AIT=treatment_AIT*sqrt(weight)
	gen _treatment_ITAIT=treatment_ITAIT*sqrt(weight)
	gen _W=sqrt(weight)
	
	rreg2 _POSTERIOR _treatment_IT _treatment_AIT _treatment_ITAIT _W, nolog gen(wgtXb2) 
	replace wgtXb2=wgtXb2*weight
	
	
	*** OLS
	reg POSTERIOR treatment_IT treatment_AIT treatment_ITAIT [aw=weight], robust
	
	foreach vv in treatment_IT treatment_AIT treatment_ITAIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval_IT_AIT=string(r(p),"%4.3f")
	
	test  treatment_IT = treatment_ITAIT
	local pval_IT_ITAIT=string(r(p),"%4.3f")
	
	test  treatment_AIT = treatment_ITAIT
	local pval_AIT_ITAIT=string(r(p),"%4.3f")
	
	post `1' ("`var'") ("`b_treatment_IT'") ("`b_treatment_AIT'") ("`b_treatment_ITAIT'") ("`pval_IT_AIT'") ("`pval_IT_ITAIT'") ("`pval_AIT_ITAIT'")  (e(N))  (string(e(r2),"%4.3f") )
	post `1' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") ("`se_treatment_ITAIT'") (" ") (" ") (" ") (.) (" ")
		
	
	*** HUBER
	reg POSTERIOR treatment_IT treatment_AIT treatment_ITAIT [aw=wgtXb2], robust
	
	foreach vv in treatment_IT treatment_AIT treatment_ITAIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval_IT_AIT=string(r(p),"%4.3f")
	
	test  treatment_IT = treatment_ITAIT
	local pval_IT_ITAIT=string(r(p),"%4.3f")
	
	test  treatment_AIT = treatment_ITAIT
	local pval_AIT_ITAIT=string(r(p),"%4.3f")
	
	post `2' ("`var'") ("`b_treatment_IT'") ("`b_treatment_AIT'") ("`b_treatment_ITAIT'") ("`pval_IT_AIT'") ("`pval_IT_ITAIT'") ("`pval_AIT_ITAIT'")  (e(N))  (string(e(r2),"%4.3f") )
	post `2' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") ("`se_treatment_ITAIT'") (" ") (" ") (" ") (.) (" ")
	
}

*===============================================================================
*			regressions forvariables without priors
*===============================================================================

gen MORT=QJH16 if QJH16~=99
gen UNSURE=(QJH16==99)
gen credibility = QJH17_1

foreach var in credibility MORT UNSURE {
	noisily di "`var'"
	
	capture drop POSTERIOR
	gen POSTERIOR=`var'
	

	capture drop wgtXb2
	
	capture drop _*
	gen _POSTERIOR=POSTERIOR*sqrt(weight)
	gen _treatment_IT=treatment_IT*sqrt(weight)
	gen _treatment_AIT=treatment_AIT*sqrt(weight)
	gen _treatment_ITAIT=treatment_ITAIT*sqrt(weight)
	
	gen _W=sqrt(weight)
	
	capture {
		rreg2 _POSTERIOR  _treatment_IT _treatment_AIT _treatment_ITAIT _W, nolog gen(wgtXb2)
		replace wgtXb2=wgtXb2*weight
	}
	if _rc~=0 {
	    gen wgtXb2=weight
	}
	*** OLS
	reg POSTERIOR treatment_IT treatment_AIT treatment_ITAIT [aw=weight], robust
	
	foreach vv in  treatment_IT treatment_AIT treatment_ITAIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval_IT_AIT=string(r(p),"%4.3f")
	
	test  treatment_IT = treatment_ITAIT
	local pval_IT_ITAIT=string(r(p),"%4.3f")
	
	test  treatment_AIT = treatment_ITAIT
	local pval_AIT_ITAIT=string(r(p),"%4.3f")
	
	post `1' ("`var'") ("`b_treatment_IT'") ("`b_treatment_AIT'") ("`b_treatment_ITAIT'") ("`pval_IT_AIT'") ("`pval_IT_ITAIT'") ("`pval_AIT_ITAIT'")  (e(N))  (string(e(r2),"%4.3f") )
	post `1' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") ("`se_treatment_ITAIT'") (" ") (" ") (" ") (.) (" ")
			
	
	*** HUBER
	reg POSTERIOR treatment_IT treatment_AIT treatment_ITAIT [aw=wgtXb2], robust
	
	foreach vv in treatment_IT treatment_AIT treatment_ITAIT {
	    local b_`vv'=string(_b[`vv'],"%4.3f")
		local se_`vv'="(" + string(_se[`vv'],"%4.3f") + ")"
		
		local p1=_b[`vv']/_se[`vv']
		
		if abs(`p1')>2.62 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.98 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
		if abs(`p1')>1.65 & `p1'~=. local b_`vv'="`b_`vv''" + "*"
	}
	
	test  treatment_IT = treatment_AIT
	local pval_IT_AIT=string(r(p),"%4.3f")
	
	test  treatment_IT = treatment_ITAIT
	local pval_IT_ITAIT=string(r(p),"%4.3f")
	
	test  treatment_AIT = treatment_ITAIT
	local pval_AIT_ITAIT=string(r(p),"%4.3f")
	
	post `2' ("`var'") ("`b_treatment_IT'") ("`b_treatment_AIT'") ("`b_treatment_ITAIT'") ("`pval_IT_AIT'") ("`pval_IT_ITAIT'") ("`pval_AIT_ITAIT'")  (e(N))  (string(e(r2),"%4.3f") )
	post `2' (" ")   ("`se_treatment_IT'") ("`se_treatment_AIT'") ("`se_treatment_ITAIT'") (" ") (" ") (" ") (.) (" ")
	
}



postclose `1'
postclose `2'

